#include "DataInfoManagerContainer.h"
#include "../IO/FileSerializer.h"
#include "ItemInfo.h"
#include "XXXX.h"
#include "ItemInfo2D.h"
#include "ItemInfo3D.h"

using namespace StaticData;

CDataInfoManagerContainer::CDataInfoManagerContainer()
	: m_rLogger(Logger::GetInstance("StaticData"))
{
	_Initialize();
	_InitAllDataInfoManager();
}

CDataInfoManagerContainer::~CDataInfoManagerContainer()
{

}

void CDataInfoManagerContainer::_Initialize()
{
#define _INIT_NAMEMANAGER(Manager) \
	m_ManagerNameMap.insert(std::pair < std::string, IDataInfoManager * > (#Manager, &Manager)); \
	m_ManagerMap.insert(std::pair < IDataInfoManager *, std::string > (&Manager, #Manager))

	_INIT_NAMEMANAGER(ItemInfoManager());
	_INIT_NAMEMANAGER(ItemInfo2DManager());
	_INIT_NAMEMANAGER(ItemInfo3DManager());
	_INIT_NAMEMANAGER(XXXXManager());
}

void CDataInfoManagerContainer::_InitAllDataInfoManager()
{
	ItemInfoManager().Initialize(10000, 0, 100);
	ItemInfo2DManager().Initialize(10000, 10, 0, 0, 100);
	ItemInfo3DManager().Initialize(10, 10, 10, 0, 0, 0);
	XXXXManager().Initialize(10000, 0, 100);
}

void CDataInfoManagerContainer::ResetAllDataInfoManager()
{
	std::map< IDataInfoManager *, std::string >::iterator it = m_ManagerMap.begin();
	for (; it!=m_ManagerMap.end(); ++it)
	{
		if (it->first)
		{
			it->first->Reset();
		}
	}
}

void CDataInfoManagerContainer::LoadFromFile(const std::string & strFileName)
{
	CFileSerializer FS(true, false);
	FS.Initialize(strFileName);

	int nSize = 0;
	FS.Serialize(nSize);
	for (int i=0; i<nSize; i++)
	{
		std::string strManagerName;
		FS.Serialize(strManagerName);
		IDataInfoManager * pDIM = GetDataInfoManager(strManagerName);
		if (pDIM)
		{
			pDIM->Serialize(FS);
		}
	}
	FS.Close();
}
void CDataInfoManagerContainer::SaveToFile(std::string & strFileName)
{
	CFileSerializer FS(true, true);
	FS.Initialize(strFileName);

	int nSize = m_ManagerMap.size();
	FS.Serialize(nSize);

	std::map< IDataInfoManager *, std::string >::iterator it = m_ManagerMap.begin();
	for (; it!=m_ManagerMap.end(); ++it)
	{
		FS.Serialize(it->second);
		it->first->Serialize(FS);
	}
	FS.Close();
}

IDataInfoManager * CDataInfoManagerContainer::GetDataInfoManager(std::string & strManagerName) const
{
	std::map< std::string, IDataInfoManager * >::const_iterator it = m_ManagerNameMap.find(strManagerName);
	if (it != m_ManagerNameMap.end())
	{
		return it->second;
	}
	else
	{
		LOG_ERROR(m_rLogger, "Get DataInfoManager Failed : " << strManagerName);
		return NULL;
	}
}
//std::string CDataInfoManagerContainer::GetDataInfoManagerName(IDataInfoManager * pDIM) const
//{
//	std::map< IDataInfoManager *, std::string >::const_iterator it = m_ManagerMap.find(pDIM);
//	if (it != m_ManagerMap.end())
//	{
//		return it->second;
//	}
//	else
//	{
//		return "";
//	}
//}

//const std::map < IDataInfoManager *, std::string > & CDataInfoManagerContainer::GetManagers()
//{
//	return m_ManagerMap;
//}
